/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 */
'use strict';
import React, {
    Component,
} from 'react';

import {
    StyleSheet,
    Text,
    View,
    Image,
    TouchableHighlight,
    NetInfo,
    Dimensions,
} from 'react-native';

import {Actions,ActionConst} from 'react-native-router-flux';
import CommonStyles from '../styles/common';
import Colors from '../styles/colors';
import LoadingView from '../components/loadingView';
import ScanBarCode from '../components/scanBarCode';
import { ScanBarCodeResult,ScanBarCodeClean} from '../actions/app';

let scanComplete = false;
export default class Scan extends Component {
    constructor (props) {
      super(props)
    }
    
    componentDidMount() {
        scanComplete = false;
        this.props.dispatch(ScanBarCodeClean());
    }

    componentWillUnmount() {
        scanComplete = false;
    }

    onBarCodeRead(e){
        if(!scanComplete){
            scanComplete = true;
            if(e.data==''){
                this.props.dispatch(ScanBarCodeClean());
            }
            else{
                this.props.dispatch(ScanBarCodeResult(e.data));
            }
            Actions.pop();
        }
    }

    render() {
      if(!scanComplete){
        return (
            <ScanBarCode
                onBarCodeRead={(data)=>this.onBarCodeRead(data)}
                finderBorderColer = {{borderColor:Colors.default_theme}}
            />
        );
      }
      else{
        return (
            <LoadingView isLoading={this.props.isLoading} tip='扫描中...'/>
        );
      }
    }
}
